//
//  DnDView.m
//  Kelindo
//
//  Created by Gianluca Puglia on 29/03/13.
//  Copyright (c) 2013 Gianluca Puglia. All rights reserved.
//

#import "DnDView.h"
#import "AppDelegate.h"

@implementation DnDView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
        highlight = NO;
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    if ( highlight ) {
        [[NSColor yellowColor] set];
        [NSBezierPath setDefaultLineWidth: 5];
        [NSBezierPath strokeRect:dirtyRect];
    }
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo> )sender {
    NSPasteboard *pboard = [sender draggingPasteboard];
    NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];

    if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
        if (sourceDragMask & NSDragOperationLink) {
            return NSDragOperationLink;
        } 
        highlight=YES;
        [self setNeedsDisplay:YES];
    }
    return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo> )sender {
    highlight=NO;
    [self setNeedsDisplay:YES];
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
    if ( [sender draggingSource] != self ) {
        NSURL *fileURL=[NSURL URLFromPasteboard:[sender draggingPasteboard]];
        AppDelegate *del = [[NSApplication sharedApplication] delegate];
        if (del.onlyConversion.state == NSOnState) {
            [[BridgeClass sharedInstance] startWithPath:fileURL.relativePath onlyConversion:YES];
        }
        else {
            [[BridgeClass sharedInstance] startWithPath:fileURL.relativePath onlyConversion:NO];
        }
    }
    return YES;
}

@end
